<?php

namespace app\common\logic;

/**
 * 友情链接逻辑
 */
class Friendship extends LogicBase
{

    /**
     * 获取友情链接列表
     */
    public function getFriendshipList($where = [], $field = 'a.*', $order = '')
    {
        
        $this->modelFriendship->alias('a');
        
         $join = [
                    [SYS_DB_PREFIX . 'picture p', 'a.cover_id = p.id'],
                ];
         
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelFriendship->getList($where, $field, $order, DB_LIST_ROWS,$join);
    }
    
    /**
     * 获取友情链接列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.friend_tittle|a.friend_content'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
     /**
     * 友情链接信息编辑
     */
    public function friendshipEdit($data = [])
    {
        $validate_result = $this->validateFriendship->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateFriendship->getError()];
        }
        
        $url = url('friendshipList');   
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        if($handle_text == '新增'){
            
            $data['created_at'] = time();
        }
        
        $data['updated_at'] = time();

        $result = $this->modelFriendship->setInfo($data);

        $result && action_log($handle_text, '友情链接' . $handle_text . '，name：' . $data['friend_tittle']);
        
        return $result ? [RESULT_SUCCESS, '友情链接操作成功', $url] : [RESULT_ERROR, $this->modelFriendship->getError()];
    }
  
     /**
     * 获取友情链接信息
     */
    public function getFriendshipInfo($where = [], $field = 'a.*')
    {
        $this->modelFriendship->alias('a');
       
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelFriendship->getInfo($where, $field);
    }
    
    
    /**
     * 编辑站点设置
     */
    public function stationEdit($data=[])
    {
        $validate_result = $this->validateWebSetting->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateWebSetting->getError()];
        }
        
        $url = url('stationSet');   
        
        $data['web_tittle'] = html_entity_decode($data['web_tittle']);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';

        if($handle_text == '新增'){
            
            $data['created_at'] = time();
        }
        
        $data['updated_at'] = time();

        $result = $this->modelWebSetting->setInfo($data);
  
        $result && action_log($handle_text, '站点设置' . $handle_text . '，name：' . $data['web_tittle']);
  
        return $result ? [RESULT_SUCCESS, '站点设置操作成功', $url] : [RESULT_ERROR, $this->modelWebSetting->getError()];
    }
    
    
    
    /**
     * 获取站点设置信息
     */
     public function getSettingInfo($where = [], $field = '*')
    {
        return $this->modelWebSetting->getInfo($where, $field);
    }
}

